package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author ll
 */
@Getter
public enum PowerIndex implements EnumTrans<CompleteEntity.PowerIndex, PowerIndex> {
    /**
     * 电源1
     */
    POWER_INDEX_1(CompleteEntity.PowerIndex.POWER_INDEX_1, 0),
    /**
     * 电源2
     */
    POWER_INDEX_2(CompleteEntity.PowerIndex.POWER_INDEX_2, 1),
    /**
     * 电源3
     */
    POWER_INDEX_3(CompleteEntity.PowerIndex.POWER_INDEX_3, 2),
    /**
     * 电源4
     */
    POWER_INDEX_4(CompleteEntity.PowerIndex.POWER_INDEX_4, 3),
    /**
     * 电源5
     */
    POWER_INDEX_5(CompleteEntity.PowerIndex.POWER_INDEX_5, 4),
    /**
     * 电源6
     */
    POWER_INDEX_6(CompleteEntity.PowerIndex.POWER_INDEX_6, 5),
    /**
     * 电源7
     */
    POWER_INDEX_7(CompleteEntity.PowerIndex.POWER_INDEX_7, 6),
    /**
     * 电源8
     */
    POWER_INDEX_8(CompleteEntity.PowerIndex.POWER_INDEX_8, 7),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.PowerIndex powerIndex;
    /**
     * 电源序号
     */
    private final Integer index;

    PowerIndex(CompleteEntity.PowerIndex powerIndex, Integer index) {
        this.id = powerIndex.getNumber();
        this.name = powerIndex.name();
        this.powerIndex = powerIndex;
        this.index = index;
    }

    /**
     * @param powerIndex powerIndex
     * @return 对应的类型
     */
    public static PowerIndex getScheduleTypeStruct(CompleteEntity.PowerIndex powerIndex) {
        Objects.requireNonNull(powerIndex, "powerIndex can not be null.");
        return Arrays.stream(PowerIndex.values())
                .filter(e -> e.name.equals(powerIndex.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.PowerIndex toProto() {
        return this.powerIndex;
    }

    @Override
    public PowerIndex formProto(CompleteEntity.PowerIndex powerIndex) {
        return getScheduleTypeStruct(powerIndex);
    }
}

